import java.io.FileOutputStream;
import java.io.PrintStream;

import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import jpcap.packet.Packet;

class capture_data
{
	int seq_num;
	int len;
	long timestamp;
	
	capture_data()
    {
        seq_num = 0;
        len = 0;
        timestamp = 0;
    }
}

public class Capture_pak 
{	
	public static void main(String args[]) throws java.io.IOException
	{
		final int TRACE_SIZE = 125000;
		capture_data[] trace_data = new capture_data[TRACE_SIZE];
		final String mynewLine = System.getProperty("line.separator").toString();
		//Obtain the list of network interfaces
		NetworkInterface[] devices = JpcapCaptor.getDeviceList();

	    //for each network interface
		System.out.println(devices.length);
	    for (int i = 0; i < devices.length; i++) 
	    {
	      //print out its name and description
	      System.out.println(i+": "+devices[i].name + "(" + devices[i].description+")");

	      //print out its datalink name and description
	      System.out.println(" datalink: "+devices[i].datalink_name + "(" + devices[i].datalink_description+")");

	      //print out its MAC address
	      System.out.print(" MAC address:");
	      for (byte b : devices[i].mac_address)
	      {
	        System.out.print(Integer.toHexString(b&0xff) + ":");
	      }
	      System.out.println();

	      //print out its IP address, subnet mask and broadcast address
	      for (NetworkInterfaceAddress a : devices[i].addresses)
	        System.out.println(" address:"+a.address + " " + a.subnet + " "+ a.broadcast);
	    }
	     
	    //Open an interface
	    int index = 1;// set index of the interface that you want to open.
	    JpcapCaptor captor=JpcapCaptor.openDevice(devices[index], 65535, false, 0);
	    System.out.println("interface" +index+" opened");
	    
	    System.out.println("Pkt" + "\t" + "TimeStamp" + "\t" + "Packet_length" + mynewLine);
	   
	    FileOutputStream fout =  new FileOutputStream("C:/Users/suraj/Desktop/newTraceFiles/Trace_skype_call.txt");
		PrintStream pout = null;
		pout = new PrintStream (fout);
		pout.printf("Pkt" + "\t" + "TimeStamp" + "\t" + "Packet_length" + mynewLine);
		
		int packlen;
	    Packet pak;	
	    for(int i=0; i<TRACE_SIZE; i++)
	    {
	    	pak = captor.getPacket();
	    	if(pak != null)
	    	{
	    		//captor.setFilter("udp", true);
		    	packlen = pak.len;
		    	System.out.println(i+ "\t" + (pak.sec*1000000) + pak.usec + "\t" + packlen  + "\n" );
		    	trace_data[i] = new capture_data();
		    	trace_data[i].seq_num = i;
		    	trace_data[i].timestamp = (pak.sec*1000000) + pak.usec;
		    	trace_data[i].len = packlen;
	    	}	
	    }
	  //write all the trace into the opened file
    
	    for (int i= 0; i < TRACE_SIZE ; i++)
	    {
	    	if(trace_data[i] != null )
	    	{
	    		pout.printf(trace_data[i].seq_num + "\t" + trace_data[i].timestamp + "\t" + trace_data[i].len + "\t" + mynewLine );
	    	}
	    }
	    System.out.println("done writing to the file. End program");
	    pout.close();
	}

}
	       